package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.Prob;
import cn.wolfcode.luowowo.comment.domain.ProblemComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.ProblemCommentQuery;
import cn.wolfcode.luowowo.comment.repository.ProblemCommentRepository;
import cn.wolfcode.luowowo.comment.service.IProblemCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProblemCommentServiceImpl implements IProblemCommentService {

    @Autowired
    private ProblemCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public Page query(ProblemCommentQuery qo) {
        //1.创建查询对象
        Query query = new Query();
        //2.封装条件查询对象
        Criteria criteria = Criteria.where("probId").is(qo.getProbId());
        query.addCriteria(criteria);
        //3.查询总条数
        long count = template.count(query, ProblemComment.class);
        //long count = repository.count(query, TravelComment.class);------>这个方法不行
        //4.封装分页查询对象
        //Pageable--->拿它的实现类
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(),
                Sort.Direction.DESC,
                "createTime");
        query.with(pageRequest);
        //5.查询list
        List<ProblemComment> list = template.find(query, ProblemComment.class);
        //返回Page实现类
        //PageImpl(List<T> content, Pageable pageable, long total)
        return new PageImpl(list,pageRequest,count);
    }

    @Override
    public void commentAdd(ProblemComment comment) {
        repository.save(comment);
    }

    @Override
    public Long count(Long probId) {
        //2.查询总条数
        Query query = new Query();
        //2.封装条件查询对象
        Criteria criteria = Criteria.where("probId").is(probId);
        query.addCriteria(criteria);
        long count = template.count(query, ProblemComment.class);
        return count;
    }

    @Override
    public List<ProblemComment> getNewTop10(Long id) {
        Query query = new Query();
        Criteria criteria = Criteria.where("probId").is(id);
        query.addCriteria(criteria);
        PageRequest pageRequest = PageRequest.of(0, 9, Sort.Direction.DESC,"createTime");
        query.with(pageRequest);
        List<ProblemComment> list = template.find(query, ProblemComment.class);
        return list;
    }

    /**
     * 增加回答
     * @param problemComment
     */
    @Override
    public void sava(ProblemComment problemComment) {
        problemComment.setCreateTime(new Date());
        repository.save(problemComment);
    }

    @Override
    public List<Prob> getshuju(Long rid) {
        if(rid==1){
            Query query = new Query();
           PageRequest pageRequest = PageRequest.of(0,
                   9,
                   Sort.Direction.DESC,
                   "jinnum");
           query.with(pageRequest);
           //5.查询list
           List<ProblemComment> list = template.find(query, ProblemComment.class);
           List<Prob> plist = new ArrayList<>();
            for (ProblemComment pr : list) {
                Prob p = new Prob();
                p.setUsername(pr.getUsername());
                p.setHeadUrl(pr.getHeadUrl());
                p.setLevel(pr.getLevel());
                p.setPaicount(pr.getJinnum());
                plist.add(p);
            }
            System.out.println(plist);
            return plist;
       }else if(rid==2){
            Query query = new Query();
            PageRequest pageRequest = PageRequest.of(0,
                    9,
                    Sort.Direction.DESC,
                    "answernum");
            query.with(pageRequest);
            //5.查询list
            List<ProblemComment> list = template.find(query, ProblemComment.class);
            List<Prob> plist = new ArrayList<>();
            for (ProblemComment pr : list) {
                Prob p = new Prob();
                p.setUsername(pr.getUsername());
                p.setHeadUrl(pr.getHeadUrl());
                p.setLevel(pr.getLevel());
                p.setPaicount(pr.getAnswernum());
                plist.add(p);
            }
            return plist;
       }else{
            Query query = new Query();
            PageRequest pageRequest = PageRequest.of(0,
                    9,
                    Sort.Direction.DESC,
                    "thumbupnum");
            query.with(pageRequest);
            //5.查询list
            List<ProblemComment> list = template.find(query, ProblemComment.class);
            List<Prob> plist = new ArrayList<>();
            for (ProblemComment pr : list) {
                Prob p = new Prob();
                p.setUsername(pr.getUsername());
                p.setHeadUrl(pr.getHeadUrl());
                p.setLevel(pr.getLevel());
                p.setPaicount(pr.getThumbupnum());
                plist.add(p);
            }
            return plist;
       }

    }
}
